package com.mmall.controller.backend;


import com.mmall.common.ServerResponse;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller("/manage/category")
public class ManageCategoryController {


    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;


    //getCategory 获取第一级的孩子节点，不递归
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getCategory(HttpSession session,
                                      @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        return iCategoryService.getChildrenCategoryPoint(categoryId);

    }

    //addcategory
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName,
                                      @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        return iCategoryService.addCategory(categoryName, parentId);
    }

    //setcategoryName
    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        return iCategoryService.setCategoryName(categoryId,categoryName);
    }

    //getdeepcategoey
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getDeepCategory(HttpSession session,
                                          @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        return iCategoryService.getSelfAndChildrenByCategoryId(categoryId);
    }
}
